"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2020-08-28 12:21:31
 * @ Modified on: 2020-09-29 10:45:52
 *
 * @version 1.3
 */
/**
 * Copyright (c) 2020 MSay2, Yoann Meclot - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSMethod = void 0;
const ArrayList_js_1 = require("../utils/ArrayList.js");
const Exceptions_js_1 = require("../utils/Exceptions.js");
const LinkedMap_js_1 = require("../utils/LinkedMap.js");
const Utils_js_1 = require("../utils/Utils.js");
const TSPoet_js_1 = require("./TSPoet.js");
/**
 * Init a new method.
 *
 * @class TSMethod
 * @version 1.2
 */
class TSMethod {
    constructor(builder) {
        this.name = builder.name;
        this.type = builder.type;
        this.annotations = builder.annotations;
        this.modifiers = builder.modifiers;
        this.statments = builder.statements;
        this.parameters = builder.parameters;
        this.codeBlocks = builder.codeBlocks;
        this.docs = builder.docs;
        this.transtypeClasses = builder.transtypeClasses;
    }
    /**
     * Init a new constructor method.
     *
     * @type {Builder}
     * @since 1.1
     */
    static constructorBuilder() {
        return new Builder(TSMethod.CONSTRUCTOR);
    }
    /**
     * Init a new method.
     *
     * @param name The name of the method.
     * @throws {NullPointerException} Check if the name is null.
     * @type {Builder}
     * @since 1.0
     */
    static methodBuilder(name) {
        return new Builder(name);
    }
    emit(codeWriter) {
        if (this.modifiers.contains(TSPoet_js_1.Modifier.ABSTRACT)) {
            codeWriter.emitAbstractMethod(this);
        }
        else {
            codeWriter.emitMethod(this);
        }
        return codeWriter;
    }
}
exports.TSMethod = TSMethod;
TSMethod.CONSTRUCTOR = "ms:_constructor_";
/**
 * @version 1.2
 */
class Builder {
    constructor(name) {
        this.annotations = new ArrayList_js_1.ArrayList();
        this.modifiers = new ArrayList_js_1.ArrayList();
        this.statements = new ArrayList_js_1.ArrayList();
        this.parameters = new LinkedMap_js_1.LinkedMap();
        this.codeBlocks = new ArrayList_js_1.ArrayList();
        this.docs = new ArrayList_js_1.ArrayList();
        this.comments = new ArrayList_js_1.ArrayList();
        this.transtypeClasses = new ArrayList_js_1.ArrayList();
        if (name == TSMethod.CONSTRUCTOR) {
            name = "constructor";
        }
        this.name = Utils_js_1.Utils.checkNotNull(name, "TSMethod must have a non-null method name.");
    }
    /**
     * Add a modifiers for the method.
     *
     * @param modifiers The modifiers to be added.
     * @throws {IllegalArgumentException} If the modifiers contain the static, export, abstract modifiers for the constructor.
     * @type {Builder}
     * @since 1.0
     */
    addModifiers(...modifiers) {
        this.modifiers = ArrayList_js_1.Arrays.asList(Utils_js_1.Utils.checkNotNull(modifiers, "TSMethod must have a non-null modifiers."));
        if (this.name == TSMethod.CONSTRUCTOR && (this.modifiers.contains(TSPoet_js_1.Modifier.STATIC)
            || this.modifiers.contains(TSPoet_js_1.Modifier.EXPORT)
            || this.modifiers.contains(TSPoet_js_1.Modifier.ABSTRACT))) {
            throw new Exceptions_js_1.IllegalArgumentException("Constructor cannot have a STATIC modifier, EXPORT modifier or ABSTRACT modifier.");
        }
        return this;
    }
    /**
     * Add a documentation.
     *
     * @param arg Your comment.
     * @type {Builder}
     * @since 1.2
     */
    addDoc(arg) {
        this.docs.add(Utils_js_1.Utils.checkNotNull(arg, "TSMethod must have a non-null doc."));
        return this;
    }
    /**
     * Add a annotation for the method.
     *
     * @param tsAnnotation The annotation to be added.
     * @throws {IllegalStateException} If you attempt annotated the constructor
     * @type {Builder}
     * @since 1.0
     */
    addAnnotation(tsAnnotation) {
        if (this.name == TSMethod.CONSTRUCTOR) {
            throw new Exceptions_js_1.IllegalStateException("Constructor cannot have a annotaion.");
        }
        this.annotations.add(Utils_js_1.Utils.checkNotNull(tsAnnotation, "TSMethod must have a non-null annotation."));
        return this;
    }
    /**
     * Add a statement.
     *
     * @param arg Your code line to be added.
     * @throws {NullPointerException} Check if the arg is null.
     * @type {Builder}
     * @since 1.0
     */
    addStatement(arg) {
        this.statements.add(Utils_js_1.Utils.checkNotNull(arg, "TSMethod must have a non-null statements values."));
        return this;
    }
    /**
     * Add a code block and manage it.
     *
     * @param codeBlock Your code block to be added.
     * @throws {NullPointerException} Check if the code block is null.
     * @type {Builder}
     * @since 1.0
     */
    addCodeBlock(codeBlock) {
        this.codeBlocks.add(Utils_js_1.Utils.checkNotNull(codeBlock, "TSMethod must have a non-null code block."));
        return this;
    }
    /**
     * Add a parameter.
     *
     * @param parameter The parameter to be added.
     * @throws {NullPointerException} Check if the TSParameter object is null.
     */
    addParameter(name, typeName) {
        this.parameters.add(Utils_js_1.Utils.checkNotNull(name, "TSMethod must have a non-null name."), Utils_js_1.Utils.checkNotNull(typeName, "TSMethod must have a non-null TypeName object."));
        return this;
    }
    /**
     * Transtype the method.
     *
     * @param classes The names of your classes.
     * @throws {IllegalStateException} If you attempt transtype the constructor.
     * @throws {NullPointerException} Check if the names classes is null.
     * @type {Builder}
     * @since 1.0
     */
    transtypeTo(...nameClasses) {
        if (this.name == TSMethod.CONSTRUCTOR) {
            throw new Exceptions_js_1.IllegalStateException("Constructor cannot typcast to generic classes");
        }
        this.transtypeClasses = ArrayList_js_1.Arrays.asList(Utils_js_1.Utils.checkNotNull(nameClasses, "TSMethod must have a non-null classes name."));
        return this;
    }
    /**
     * Set a return type to method.
     *
     * @param typeName The return type.
     * @throws {IllegalStateException} If you attempt to apply a return value to constructor.
     * @throws {NullPointerException} Check if the TypeName object is null.
     * @type {Builder}
     * @since 1.0
     */
    return(typeName) {
        if (this.name == TSMethod.CONSTRUCTOR) {
            throw new Exceptions_js_1.IllegalStateException("Constructor cannot have a return type.");
        }
        this.type = Utils_js_1.Utils.checkNotNull(typeName, "TSMethod must have a non-null return type.");
        return this;
    }
    /**
     * Build a method.
     *
     * @throws {NullPointerException} Check if the name is null.
     * @throws {NullPointerException} Check if the modifiers is null.
     * @throws {NullPointerException} Check if the statements is null.
     * @throws {NullPointerException} Check if the code block is null.
     * @throws {NullPointerException} Check if the parameter is null.
     * @throws {NullPointerException} Check if the names classes to transtype is null.
     * @throws {NullPointerException} Check if the return type is null.
     * @type {TSMethod}
     * @since 1.0
     */
    build() {
        return new TSMethod(this);
    }
}
//# sourceMappingURL=TSMethod.js.map